﻿using In66.Net.Core.Core.Ids;
using In66.Net.Repository.Entities;
using In66.Net.Repository.IRepositories.IEF;

namespace In66.Net.Share.Application.Tracker
{

    public class DbMessageTrackerService : IMessageTracker
    {
        public IBaseRepository<EventTracker> _trackerRepo;

        public DbMessageTrackerService(IBaseRepository<EventTracker> trackerRepo)
        {
            _trackerRepo = trackerRepo;
        }

        public TrackerKind Kind => TrackerKind.Db;

        public async Task<bool> HasProcessedAsync(string eventId, string trackerName)
        {
            return await _trackerRepo.AnyAsync(x => x.EventId == eventId && x.TrackerName == trackerName, true);
        }

        public async Task MarkAsProcessedAsync(string eventId, string trackerName)
        {
            await _trackerRepo.InsertAsync(new EventTracker
            {
                Id = IdGenerater.GetNextId(),
                EventId = eventId,
                TrackerName = trackerName
            });
        }
    }


}
